package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.transaction.Transactional;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.dao.IVideoDao;
import com.magicode.cms.dao.IVideoProgramDao;
import com.magicode.cms.entiy.base.VideoProgram;
import com.magicode.cms.service.IVideoProgramService;
import com.magicode.common.dao.ICommonDaoService;
@Service("servicevideoprogram")
@Transactional
public class VideoProgramServiceImpl extends BaseServiceImpl<VideoProgram, IVideoProgramDao> implements IVideoProgramService{
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			hql += " order by t.initialWordDic.orderCode  asc , t.orderCode asc ";
//			hql += " left join "+entity_alias_name+".readOperate as ro ";
//			hql += " left join "+entity_alias_name+".channel as ch ";
//			hql += " left join "+entity_alias_name+".author as au ";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
//			gridsel.put("shortTitle", ""+entity_alias_name+".shortTitle as shortTitle ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			gridsel.put("initialWordDic", "iwd.title as initialWordDic ");
			gridsel.put("valid", "case when "+entity_alias_name+".valid = 1 then '有效' else '无效' end "
					+ " as valid ");
			
			return this.queryMapDataList(hql, gridsel, start, size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	}

	@Override
	public List<Map<String,Object>> select(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			hql += " order by t.initialWordDic.orderCode  asc , t.orderCode asc ";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			gridsel.put("initialWordDic", "iwd.title as initialWordDic ");
			gridsel.put("valid", "case when "+entity_alias_name+".valid = 1 then '有效' else '无效' end "
					+ " as valid ");			
			return this.queryMapDataList(hql, gridsel, 0, 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql ="from " +VideoProgram.class.getName()+" t ";
		if(null!=map && !map.isEmpty()){

			hql += " left join "+entity_alias_name+".initialWordDic as iwd ";
			if(null!=map && !map.isEmpty()){
				hql += "where 1 = 1 ";
				if(map.containsKey(field_id)){
					hql = hql+" and "+entity_alias_name+".id = '"+map.get(field_id)+"'";
				}
				if(map.containsKey("title")){
					hql = hql+" and "+entity_alias_name+".title like '%"+map.get("title")+"%'";
				}
				if(map.containsKey("valid")&& (
						map.get("valid") != null && 
						map.get("valid").trim().length() > 0
						)){
					hql = hql+" and "+entity_alias_name+".valid = "+map.get("valid")+"";
				}
				if(map.containsKey("initialWordDicId") && (
						map.get("initialWordDicId") != null && 
						map.get("initialWordDicId").trim().length() > 0
						)
						){
					hql = hql+" and "+entity_alias_name+".initialWordDic.id = '"+map.get("initialWordDicId")+"'";
				}
			}
		}
		return hql;
	}
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "标题");
		map.put("width", 200);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "orderCode");
		map.put(json_title, "排序码");
		map.put("width", 50);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "initialWordDic");
		map.put(json_title, "首字母");
		map.put("width", 50);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "valid");
		map.put(json_title, "有效性");
		map.put("width", 70);
		beaninfo.add(map);
		return beaninfo;
	}
	@Override
	public IVideoProgramDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return vedioProgramDao;
	}
	/*
	@Override
	public List<JsonOperation> getOpts(String url,String id) {
		List<JsonOperation> opts = new ArrayList<JsonOperation> ();
		JsonOperation opt= new JsonOperation();
		opt.setName("修改");
		opt.setUrl(ctx.getContextPath()+"/"+url+"/getinfo/"+id+"/edit"+url);
		opt.setType("edit");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("删除");
		opt.setUrl(ctx.getContextPath()+"/"+url+"/"+id+"?_requestMethod=delete");
		opt.setType("delete2");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("预览");
		opt.setUrl(ctx.getContextPath()+"/"+url+"/preview/"+id+"");
		opt.setType("preview");
		opts.add(opt);
		return opts;
	}
*/
	@Autowired
	@Qualifier("daovideoprogram")
	private IVideoProgramDao vedioProgramDao;

	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;
	
	@Autowired
	private ServletContext ctx;

	public ServletContext getCtx() {
		return ctx;
	}
	public void setCtx(ServletContext ctx) {
		this.ctx = ctx;
	}
	public IVideoProgramDao getVedioProgramDao() {
		return vedioProgramDao;
	}
	public void setVedioProgramDao(IVideoProgramDao vedioProgramDao) {
		this.vedioProgramDao = vedioProgramDao;
	}
	public ICommonDaoService getCommonDaoService() {
		return commonDaoService;
	}
	public void setCommonDaoService(ICommonDaoService commonDaoService) {
		this.commonDaoService = commonDaoService;
	}
}
